package com.wn.icloud.provider.uac.controller;

import com.wn.icloud.common.msg.ObjectRestResponse;
import com.wn.icloud.common.util.TreeUtil;
import com.wn.icloud.provider.uac.constant.AdminCommonConstant;
import com.wn.icloud.provider.uac.entity.Group;
import com.wn.icloud.provider.uac.remote.vo.GroupOrgInfResVo;
import com.wn.icloud.provider.uac.service.GroupBiz;
import com.wn.icloud.provider.uac.service.GroupTypeService;
import com.wn.icloud.provider.uac.service.ResourceAuthorityBiz;
import com.wn.icloud.provider.uac.service.dto.GroupDto;
import com.wn.icloud.provider.uac.vo.AuthorityMenuTree;
import com.wn.icloud.provider.uac.vo.GroupTree;
import com.wn.icloud.provider.uac.vo.GroupUsers;
import com.wn.icloud.rest.BaseController;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author leeyang
 * @create 2017-06-12 8:49
 */
@Controller
@RequestMapping("/group")
@Api("群组模块")
public class GroupController extends BaseController<GroupBiz, Group> {
    @Autowired
    private ResourceAuthorityBiz resourceAuthorityBiz;

    @Autowired
    private GroupTypeService groupTypeService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Group> list(String name,String groupType) {
        if(StringUtils.isBlank(name)&&StringUtils.isBlank(groupType)) {
            return new ArrayList<Group>();
        }
        Example example = new Example(Group.class);
        if (StringUtils.isNotBlank(name)) {
            example.createCriteria().andLike("name", "%" + name + "%");
        }
        if (StringUtils.isNotBlank(groupType)) {
            example.createCriteria().andEqualTo("groupType", groupType);
        }

        return baseBiz.selectByExample(example);
    }


    //todo 文档已记录
    @RequestMapping(value = "/{id}/user", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse modifiyUsers(@PathVariable int id, String members, String leaders){
        baseBiz.modifyGroupUsers(id, members, leaders);
        return new ObjectRestResponse().rel(true);
    }
    //todo 文档已记录
    @RequestMapping(value = "/{id}/user", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<GroupUsers> getUsers(@PathVariable int id){
        return new ObjectRestResponse<GroupUsers>().rel(true).data(baseBiz.getGroupUsers(id));
    }

    //todo 文档已记录
    @RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse modifyMenuAuthority(@PathVariable  int id, String menuTrees){
        String [] menus = menuTrees.split(",");
        baseBiz.modifyAuthorityMenu(id, menus);
        return new ObjectRestResponse().rel(true);
    }
    //todo 文档已记录
    @RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<List<AuthorityMenuTree>> getMenuAuthority(@PathVariable  int id){
        return new ObjectRestResponse().data(baseBiz.getAuthorityMenu(id)).rel(true);
    }

    //todo 文档已记录
    @RequestMapping(value = "/{id}/authority/element/add", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse addElementAuthority(@PathVariable  int id,int menuId, int elementId){
        baseBiz.modifyAuthorityElement(id,menuId,elementId);
        return new ObjectRestResponse().rel(true);
    }
    //todo 文档已记录
    @RequestMapping(value = "/{id}/authority/element/remove", method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse removeElementAuthority(@PathVariable int id,int menuId, int elementId){
        baseBiz.removeAuthorityElement(id,menuId,elementId);
        return new ObjectRestResponse().rel(true);
    }

    @RequestMapping(value = "/{id}/authority/element", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<List<Integer>> getElementAuthority(@PathVariable  int id){
        return new ObjectRestResponse().data(baseBiz.getAuthorityElement(id)).rel(true);
    }


    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public List<GroupTree> tree(String name, String groupType) {
        if(StringUtils.isBlank(name)&&StringUtils.isBlank(groupType)) {
            return new ArrayList<GroupTree>();
        }
        Example example = new Example(Group.class);
        if (StringUtils.isNotBlank(name)) {
            example.createCriteria().andLike("name", "%" + name + "%");
        }
        if (StringUtils.isNotBlank(groupType)) {
            example.createCriteria().andEqualTo("groupType", groupType);
        }
        return  getTree(baseBiz.selectByExample(example), AdminCommonConstant.ROOT);
    }

    //查询所有车间
    @RequestMapping(value = "/getOrganize", method = RequestMethod.GET)
    @ResponseBody
    public List<Group> organize() {
        Example example = new Example(Group.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("parentId",AdminCommonConstant.BASE_GROUP_ORGANIZE);
        criteria.andEqualTo("groupType",AdminCommonConstant.BASE_GROUP_ORGANIZE_TYPE);
        List<Group> groups = baseBiz.selectByExample(example);
        return groups;
    }
    //根据车间id查询车间
    @RequestMapping(value = "/getOrganizeById", method = RequestMethod.GET)
    @ResponseBody
    public List<Group> organizeById(Integer id) {
        Example example = new Example(Group.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("parentId",AdminCommonConstant.BASE_GROUP_ORGANIZE);
        criteria.andEqualTo("groupType",AdminCommonConstant.BASE_GROUP_ORGANIZE_TYPE);
        criteria.andEqualTo("id",id);
        List<Group> groups = baseBiz.selectByExample(example);
        return groups;
    }

    //查询所有车间
    @RequestMapping(value = "/getAllOrganize", method = RequestMethod.GET)
    @ResponseBody
    public List<GroupDto> allOrganize() {
        Example example = new Example(Group.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("parentId",AdminCommonConstant.BASE_GROUP_ORGANIZE);
        criteria.andEqualTo("groupType",AdminCommonConstant.BASE_GROUP_ORGANIZE_TYPE);
        List<Group> groups = baseBiz.selectByExample(example);
        List<GroupDto> groupDtos = new ArrayList<>();
        for (Group group : groups){
            GroupDto groupDto = new GroupDto();
            BeanUtils.copyProperties(group,groupDto);
            groupDtos.add(groupDto);
        }
        return groupDtos;
    }


    private List<GroupTree> getTree(List<Group> groups,int root) {
        List<GroupTree> trees = new ArrayList<GroupTree>();
        GroupTree node = null;
        for (Group group : groups) {
            node = new GroupTree();
            node.setLabel(group.getName());
            BeanUtils.copyProperties(group, node);
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }

    /**
     * 获取组织车间数据
     * @param
     * @return List<GroupOrgInfResVo>
     */
    @RequestMapping(value = "/groupOrgInfo", method = RequestMethod.GET)
    @ResponseBody
    public List<GroupOrgInfResVo> getGroupOrgInfo(){
        Integer groupType = groupTypeService.getOrgInfoType();
        Example example = new Example(Group.class);
        example.createCriteria().andNotEqualTo("parentId", -1);
        example.createCriteria().andEqualTo("groupType", groupType);
        List<Group> groups = baseBiz.selectByExample(example);
        List<GroupOrgInfResVo> response = new ArrayList<>();
        for(Group group : groups){
            if(groupType == group.getGroupType()){
                if(-1 == group.getParentId()){
                    continue;
                } else{
                    GroupOrgInfResVo vo = new GroupOrgInfResVo();
                    vo.setValue(group.getCode());
                    vo.setLabel(group.getName());
                    response.add(vo);
                }
            }
        }
        return response;
    }
}
